package fr.afcepf.atod18.fw.controler;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import fr.afcepf.atod18.fw.action.ActionConnexion;
import fr.afcepf.atod18.fw.action.ActionIndex;
import fr.afcepf.atod18.fw.action.ActionInscription;
import fr.afcepf.atod18.fw.action.ActionLogout;
import fr.afcepf.atod18.fw.action.ActionWelcome;
import fr.afcepf.atod18.fw.action.IAction;
import fr.afcepf.atod18.fw.entities.Action;
import fr.afcepf.atod18.fw.entities.Form;
import fr.afcepf.atod18.fw.exception.FwException;
import fr.afcepf.atod18.fw.form.ConnexionForm;
import fr.afcepf.atod18.fw.form.IActionForm;
import fr.afcepf.atod18.fw.form.IndexForm;
import fr.afcepf.atod18.fw.form.InscriptionForm;
import fr.afcepf.atod18.fw.form.LogoutForm;
import fr.afcepf.atod18.fw.form.WelcomeForm;
import fr.afcepf.atod18.fw.parser.ParserMapInit;
import fr.afcepf.atod18.fw.parser.ParserXml;

/**
 * Servlet implementation class ServletDispatch
 * Cette servlet constitue le contôleur
 */
public class ActionServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private Map<String, Class<IAction>> mapAction;
    private Map<String, Class<IActionForm>> mapActionForm;
    private Map<String, String> mapActionErr;
    Factory factory;
    FactoryActionForm factoryActionForm;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ActionServlet() {
        super();
    }
        
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    
        ParserMapInit parserMapInit;
        try {
            parserMapInit = new ParserMapInit(getServletContext().getInitParameter("nomFicParam"));
            parserMapInit.initMaps();
            mapAction = parserMapInit.getMapAction();
            mapActionForm = parserMapInit.getMapActionForm();
            mapActionErr = parserMapInit.getMapActionErr();
        } catch (FwException fwe) {
            fwe.printStackTrace();
        }
        
        
        factory  = new Factory();
        factoryActionForm  = new FactoryActionForm();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        String suite;

        //String provenance = request.getServletPath().replace("/pages","");
        String provenance = request.getServletPath();
        System.out.println("provenance=" + provenance);
        
        // map des champs de saisie
        Map<String, String[]> mapParam = request.getParameterMap();
        // appel à la factoryActionMap pour instancier la ActionForm
        IActionForm formAction = factoryActionForm.done(provenance, mapActionForm);
        // Remplir la ActionForm avec les données provenant de la saisie
        MyBeanPopulate.populateBean(formAction, mapParam);

        // appel à la factory pour instancier l'action
        IAction a = factory.done(provenance, mapAction );
        
        // Execute action si ActionForm est validé (champs de saisies bien remplie)
        if (formAction.validateForm())
            suite = a.execute(request, response);
        else
            // Recherche dans la map jsp associée
            suite = mapActionErr.get(request.getServletPath());
            
        // Mise à jour dans la jsp avec un message éventuel
        request.setAttribute(formAction.getClass().getSimpleName(), formAction);
        
        request.getRequestDispatcher(suite).forward(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet( request, response);
    }
 
}
